<template>
  <div class="password-change-container">
    <div class="form-wrapper">
      <el-form 
        :model="formData" 
        :rules="rules" 
        ref="formRef" 
        label-width="120px"
        class="password-form"
      >
        <el-form-item label="账号" prop="phone">
          <el-input v-model="formData.phone" placeholder="请输入账号"></el-input>
        </el-form-item>
        
        <el-form-item label="原密码" prop="oldPassword">
          <el-input
            v-model="formData.oldPassword"
            type="password"
            placeholder="请输入原密码"
            show-password
          />
        </el-form-item>
        
        <el-form-item label="新密码" prop="newPassword">
          <el-input
            v-model="formData.newPassword"
            type="password"
            placeholder="请输入新密码"
            show-password
          />
        </el-form-item>
        
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input
            v-model="formData.confirmPassword"
            type="password"
            placeholder="请再次输入新密码"
            show-password
          />
        </el-form-item>
        
        <el-form-item>
          <el-button 
            type="primary" 
            @click="submitForm"
            :loading="loading"
          >
            {{ loading ? '提交中...' : '确认修改' }}
          </el-button>
          <el-button @click="resetForm" style="margin-left: 10px">
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue';
import { ElMessage } from 'element-plus';
import { $postf } from "../../ajax/index.js";

// 表单数据类型定义
interface FormData {
  phone: string;
  oldPassword: string;
  newPassword: string;
  confirmPassword: string;
}

// 表单数据
const formData = reactive<FormData>({
  phone: '',
  oldPassword: '',
  newPassword: '',
  confirmPassword: ''
});

// 表单引用
const formRef = ref();

// 加载状态
const loading = ref(false);

// 表单验证规则
const rules = reactive({
  phone: [
    { required: true, message: '请输入账号', trigger: 'blur' },
    { min: 5, max: 20, message: '账号长度在5-20个字符', trigger: 'blur' }
  ],
  oldPassword: [
    { required: true, message: '请输入原密码', trigger: 'blur' },
    { min: 6, message: '密码长度至少6位', trigger: 'blur' }
  ],
  newPassword: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    { min: 8, message: '密码长度至少8位', trigger: 'blur' },
    { 
      validator: (rule: any, value: string, callback: any) => {
        if (value && value === formData.oldPassword) {
          callback(new Error('新密码不能与原密码相同'));
        } else {
          callback();
        }
      }, 
      trigger: 'blur' 
    }
  ],
  confirmPassword: [
    { required: true, message: '请确认新密码', trigger: 'blur' },
    { 
      validator: (rule: any, value: string, callback: any) => {
        if (value && value !== formData.newPassword) {
          callback(new Error('两次输入的密码不一致'));
        } else {
          callback();
        }
      }, 
      trigger: 'blur' 
    }
  ]
});

// 提交表单
const submitForm = () => {
  if (!formRef.value) return;
  
  formRef.value.validate((valid: boolean) => {
    if (valid) {
      loading.value = true;
      
      // 模拟API请求
      setTimeout(() => {
        // 这里调用实际的API
        $postf('/api/change-password', formData)
          .then(() => {
            ElMessage.success('密码修改成功');
            resetForm();
          })
          .catch((error: any) => {
            ElMessage.error(error.message || '密码修改失败');
          })
          .finally(() => {
            loading.value = false;
          });
      }, 1000);
    } else {
      ElMessage.warning('请检查表单信息');
      return false;
    }
  });
};

// 重置表单
const resetForm = () => {
  if (!formRef.value) return;
  formRef.value.resetFields();
};

// 页面加载时的操作
onMounted(() => {
  // 可以在这里获取用户信息
});
</script>

<style scoped>
.password-change-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f7fa;
}

.form-wrapper {
  width: 400px;
  padding: 30px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.password-form {
  margin-top: 20px;
}
</style>